package com.weikuang.fareast.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.CheckBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 审核
 */
public class CheckAdapter extends BaseRecyclerAdapter<CheckBean> {
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_check;
    }

    public CheckAdapter() {

    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        CheckBean bean = getItem(position);

        holder.getText(R.id.offic).setText(bean.offic);
        holder.getText(R.id.hospital).setText(bean.hospital);
        holder.getText(R.id.name).setText(bean.name);
        holder.getText(R.id.fold_nums).setText(bean.fold_nums);
       // holder.getText(R.id.sort_nums).setText(bean.backs+"");
        LinearLayout linearLayout = holder.getView(R.id.linear);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前点击的位置
                if (callback != null) {
                    callback.onCheckClick(position, getItem(position).check, holder);
                }
            }
        });
        //更改状态
        if(getItem(position).check){
            linearLayout.setBackgroundResource(R.drawable.yellow_text_pressed);
        }else{
            linearLayout.setBackgroundResource(R.drawable.yellow_text_normal);
        }


        EditText mExceptionEditText = holder.getView(R.id.ed_num);

        if (mExceptionEditText.getTag(R.id.ed_num) instanceof TextWatcher){
            mExceptionEditText.removeTextChangedListener(((TextWatcher)mExceptionEditText.getTag(R.id.ed_num)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.nums == 0) {
            mExceptionEditText.setText( "");
        }else {
            mExceptionEditText.setText(bean.nums + "");
        }
        mExceptionEditText.setSelection(mExceptionEditText.getText().length());
        TextWatcher textWatcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.nums = Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText.addTextChangedListener(textWatcher);
        mExceptionEditText.setTag(R.id.ed_num,textWatcher);


        EditText mExceptionEditText_loss = holder.getView(R.id.ed_num_loss);

        if (mExceptionEditText_loss.getTag(R.id.ed_num_loss) instanceof TextWatcher){
            mExceptionEditText_loss.removeTextChangedListener(((TextWatcher)mExceptionEditText_loss.getTag(R.id.ed_num_loss)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.num_loss == 0) {
            mExceptionEditText_loss.setText("");
        }else {
            mExceptionEditText_loss.setText(bean.num_loss + "");
        }
        mExceptionEditText_loss.setSelection(mExceptionEditText_loss.getText().length());
        TextWatcher textWatcher_loss=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.num_loss= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_loss.addTextChangedListener(textWatcher_loss);
        mExceptionEditText_loss.setTag(R.id.ed_num_loss,textWatcher_loss);

        EditText mExceptionEditText_backs = holder.getView(R.id.ed_num_backs);

        if (mExceptionEditText_backs.getTag(R.id.ed_num_backs) instanceof TextWatcher){
            mExceptionEditText_backs.removeTextChangedListener(((TextWatcher)mExceptionEditText_backs.getTag(R.id.ed_num_backs)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.backs == 0) {
            mExceptionEditText_backs.setText("");
        }else {
            mExceptionEditText_backs.setText(bean.backs + "");
        }
        mExceptionEditText_backs.setSelection(mExceptionEditText_backs.getText().length());
        TextWatcher textWatcher_backs=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.backs= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_backs.addTextChangedListener(textWatcher_backs);
        mExceptionEditText_backs.setTag(R.id.ed_num_backs,textWatcher_backs);

    }

    private VerifySelectback callback;
    public interface VerifySelectback{
        public void onCheckClick(int position,boolean status,CommonHolder commonHolder);
    }

    public void setCallback(VerifySelectback callback) {
        this.callback = callback;
    }
}
